window.onload = function () {
  sideBarClick();
  addTaskClick();
  changeTaskClick();
  init();
};

const sideBarClick = () => {
  const tasksToday = document.getElementById('tasksToday');
  const tasksAll = document.getElementById('tasksAll');
  const tasksDone = document.getElementById('tasksDone');
  const tasksPass = document.getElementById('tasksPass');
  const tasks = [tasksAll, tasksDone, tasksPass, tasksToday];

  bind(tasksToday, 'click', () => {
    tasks.forEach((d) => {
      d.classList.remove('active');
    });
    tasksToday.classList.add('active');
    document.getElementById('listInto').innerText = '今日任务';
    new TasksTool().toTodays().refreshAll();
  });

  bind(tasksAll, 'click', () => {
    tasks.forEach((d) => {
      d.classList.remove('active');
    });
    tasksAll.classList.add('active');
    document.getElementById('listInto').innerText = '全部任务';
    new TasksTool().refreshAll();
  });

  bind(tasksPass, 'click', () => {
    tasks.forEach((d) => {
      d.classList.remove('active');
    });
    tasksPass.classList.add('active');
    document.getElementById('listInto').innerText = '已过期';
    new TasksTool().toPass().refreshAll();
  });

  bind(tasksDone, 'click', () => {
    tasks.forEach((d) => {
      d.classList.remove('active');
    });
    tasksDone.classList.add('active');
    document.getElementById('listInto').innerText='已完成';
    new TasksTool().toDone().refreshAll();
  });
};

const addTaskClick = () => {
  const newTaskName = document.getElementById('newTaskName');
  const newTaskDate = document.getElementById('newTaskDate');
  const newTaskSubmit = document.getElementById('newTaskSubmit');

  bind(newTaskSubmit, 'click', () => {
    const taskName = newTaskName?.value.trim();
    const taskTime = newTaskDate?.value;
    if (taskName.length === 0 || !taskTime.length) {
      alert('任务名称与时间不能为空');
      return;
    }
    new Task({
      taskName,
      taskTime,
      done: false,
    }).insertLocalStorage();
    tasksReload();
    newTaskName.value = '';
    newTaskDate.value = '';
  });
};

const changeTaskClick = () => {
  const taskItems = document.getElementById('taskItems');
  bind(taskItems, 'click', (e) => {
    const target = e.target;
    if ([...target.classList].indexOf('switchDone') + 1) {
      Task.updateLocalStorage({ done: target.checked }, target.parentNode.id);
      tasksReload();
    } else if (
      [...target.classList].indexOf('remove') + 1 ||
      [...target.classList].indexOf('fa-trash-alt') + 1
    ) {
      Task.removeLocalStorage(
        target.parentNode.id || target.parentNode.parentNode.id
      );
      tasksReload();
    }
  });
  bind(taskItems, 'change', (e) => {
    const target = e.target;
    console.log(target.oldvalue);
    if ([...target.classList].indexOf('deadline') + 1) {
      if (target.value.trim().length === 0) {
        alert('请输入内容');
        e.target.value = e.target.oldvalue;
        return;
      }
      Task.updateLocalStorage({ taskTime: target.value }, target.parentNode.id);
      tasksReload();
    } else if ([...target.classList].indexOf('taskText') + 1) {
      if (target.value.trim().length === 0) {
        alert('请输入内容');
        e.target.value = e.target.oldvalue;
        return;
      }
      Task.updateLocalStorage({ taskName: target.value }, target.parentNode.id);
      tasksReload();
    }
  });
  bind(taskItems, 'focusin', (e) => {
    e.target.oldvalue = e.target.value;
  });
};

const bind = (obj, eventstr, callback) => {
  if (obj.addEventListener) obj.addEventListener(eventstr, callback, false);
  else
    obj.attachEvent('on' + eventstr, function () {
      // 解决IE this为window的问题
      callback.call(obj);
    });
};

const tasksReload = () => {
  const curTag = document.querySelector('.category.active');
  curTag.click();
};

const init = () => {
  if (!localStorage.getItem('taskInfo')) localStorage.setItem('taskInfo', '[]');
  const tasksToday = document.getElementById('tasksToday');
  tasksToday.click();
};

const init2 = () => {
  new Task({
    taskName: '吃饭',
    taskTime: '2022-01-01',
    done: false,
  }).insertLocalStorage();
  new Task({
    taskName: '睡觉',
    taskTime: '2022-03-01',
    done: true,
  }).insertLocalStorage();
  new Task({
    taskName: '打豆豆',
    taskTime: '2022-01-14',
    done: false,
  }).insertLocalStorage();
  new Task({
    taskName: '学习',
    taskTime: '2022-02-01',
    done: true,
  }).insertLocalStorage();
};
